home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Securite / Password Safe / pwsafe-3.35.exe / KPV2_to_PWS.xslt < prev    next >
Extensible Markup Language  |  2014-07-26  |  42KB  |  1,044 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3.   Copyright (c) 2003-2014 Rony Shapiro <ronys@users.sourceforge.net>.
  4.   All rights reserved. Use of the code is allowed under the
  5.   Artistic License 2.0 terms, as specified in the LICENSE file
  6.   distributed with this code, or available from
  7.   http://www.opensource.org/licenses/artistic-license-2.0.php
  8.  
  9.   This file will convert XML files exported by KeePass V2 (tested with KeePass v2.15)
  10.   into the equivalent XML files that can be imported into PasswordSafe V3.26 or later.
  11.  
  12.   The delimiter attribute in the 'passwordsafe' element is set to "right angle quotation
  13.   marks" and any carriage returns/line feeds in the Notes field will be converted this
  14.   character to enable the Notes field to be processed correctly by PasswordSafe during import.
  15.  
  16.   KeePass exports the Group Tree structure using nested groups. PasswordSafe uses a dot ('.').
  17.   Therefore this XSLT code first changes any dot in the group field to a forward slash ('/').
  18.   This will create the correct group path in Password Safe but any dots in the original
  19.   group field will remain a forward slash.
  20.  
  21.   This XSLT file conforms to V1.0 of XSLT as described in http://www.w3.org/TR/xslt
  22.  
  23.   Under Windows, the following 3 programs can process this file:
  24.   a. Command line program msxml.exe from Microsoft (Note: the executable states it
  25.      is V1.1.0.1 but the website says it is V2.0).
  26.   b. AltovaXML Community Edition (current version 2011r3). 
  27.      See http://www.altova.com/altovaxml.html.
  28.   c. Saxon-HE (Home Edition) (current version 9.3.0.5).
  29.      See http://saxon.sourceforge.net/.
  30.      There is also a graphical front-end for Saxon called Kernow.
  31.      See http://kernowforsaxon.sourceforge.net
  32.  
  33.   All programs are free.
  34. -->
  35.  
  36. <xsl:stylesheet version="1.0" 
  37.   xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  38.   xmlns:xs="http://www.w3.org/2001/XMLSchema"
  39.   exclude-result-prefixes="xs">
  40.  
  41.   <xsl:output method="xml" encoding="UTF-8" indent="yes"
  42.      cdata-section-elements="group title password username url notes uuid runcommand autotype oldpassword"/>
  43.  
  44.   <xsl:variable name="delimiter" select="'»'"/>
  45.   <xsl:variable name="crlf" select="' '"/>
  46.   <xsl:variable name="lfcr" select="' '"/>
  47.  
  48.   <xsl:variable name="loCase" select="'abcdefghijklmnopqrstuvwxyz'"/>
  49.   <xsl:variable name="upCase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
  50.  
  51.   <xsl:template match="/">
  52.     <passwordsafe>
  53.       <xsl:attribute name="xsi:noNamespaceSchemaLocation"
  54.          namespace="http://www.w3.org/2001/XMLSchema-instance">pwsafe.xsd</xsl:attribute>
  55.       <xsl:attribute name="delimiter"><xsl:value-of select="$delimiter"/></xsl:attribute>
  56.       <!-- process root group -->
  57.       <xsl:for-each select="KeePassFile/Root/Group">
  58.         <xsl:call-template name="groupTemplate">
  59.           <xsl:with-param name="group" select="''"/>
  60.         </xsl:call-template>
  61.       </xsl:for-each>
  62.       <xsl:value-of select="$crlf" disable-output-escaping="yes"/>
  63.       <xsl:value-of select="$crlf" disable-output-escaping="yes"/>
  64.     </passwordsafe>
  65.   </xsl:template>
  66.  
  67.   <!-- process group -->
  68.   <xsl:template name="groupTemplate">
  69.     <xsl:param name="group"/>
  70.     <xsl:param name="groupAutotype"/>
  71.  
  72.     <!-- build group name -->
  73.     <xsl:variable name="currentGroup">
  74.       <xsl:choose>
  75.         <xsl:when test="normalize-space($group) != ''">
  76.           <xsl:value-of  select="concat($group, '.', translate(Name,'.','/'))"/>
  77.         </xsl:when>
  78.         <xsl:otherwise>
  79.           <xsl:value-of select="translate(Name,'.','/')"/>
  80.         </xsl:otherwise>
  81.       </xsl:choose>
  82.     </xsl:variable>
  83.     <!-- set autotype -->
  84.     <xsl:variable name="currentAutotype">
  85.       <xsl:choose>
  86.         <xsl:when test="normalize-space(DefaultAutoTypeSequence) != ''">
  87.           <xsl:value-of select="DefaultAutoTypeSequence"/>
  88.         </xsl:when>
  89.         <xsl:otherwise>
  90.           <xsl:value-of select="$groupAutotype"/>
  91.         </xsl:otherwise>
  92.       </xsl:choose>
  93.     </xsl:variable>
  94.  
  95.     <!-- process elements -->
  96.     <xsl:for-each select="Entry">
  97.       <xsl:call-template name="entryTemplate">
  98.         <xsl:with-param name="group" select="$currentGroup"/>
  99.         <xsl:with-param name="groupAutotype" select="$currentAutotype"/>
  100.       </xsl:call-template>
  101.     </xsl:for-each>
  102.  
  103.     <!-- process nested groups -->
  104.     <xsl:for-each select="Group">
  105.       <xsl:call-template name="groupTemplate">
  106.         <xsl:with-param name="group" select="$currentGroup"/>
  107.         <xsl:with-param name="groupAutotype" select="$currentAutotype"/>
  108.       </xsl:call-template>
  109.     </xsl:for-each>
  110.   </xsl:template>
  111.  
  112.   <!-- process entry -->
  113.   <xsl:template name="entryTemplate">
  114.     <xsl:param name="group"/>
  115.     <xsl:param name="groupAutotype"/>
  116.  
  117.     <xsl:value-of select="$crlf" disable-output-escaping="yes"/>
  118.     <xsl:value-of select="$crlf" disable-output-escaping="yes"/>
  119.  
  120.     <entry>
  121.       <!-- group -->
  122.       <group>
  123.         <xsl:value-of select="$group"/>
  124.       </group>
  125.  
  126.       <!-- process strings (unknown string are placed into Notes) -->
  127.       <!-- title -->
  128.       <title>
  129.         <xsl:value-of select="String[Key='Title']/Value"/>
  130.       </title>
  131.       <!-- username -->
  132.       <username>
  133.         <xsl:value-of select="String[Key='UserName']/Value"/>
  134.       </username>
  135.       <!-- password -->
  136.       <password>
  137.         <xsl:value-of select="String[Key='Password']/Value"/>
  138.       </password>
  139.       <!-- url/run -->
  140.       <xsl:if test="normalize-space(String[Key='URL']/Value) != ''">
  141.         <xsl:call-template name="urlParser">
  142.           <xsl:with-param name="url" select="String[Key='URL']/Value"/>
  143.           <xsl:with-param name="useAlt">
  144.             <xsl:choose>
  145.               <xsl:when test="normalize-space(OverrideURL) = ''">
  146.                 0
  147.               </xsl:when>
  148.               <xsl:otherwise>
  149.                 1
  150.               </xsl:otherwise>
  151.             </xsl:choose>
  152.           </xsl:with-param>
  153.         </xsl:call-template>
  154.       </xsl:if>
  155.  
  156.       <!-- notes and other fields -->
  157.       <notes>
  158.         <!-- \n translated into », \r removed -->
  159.         <xsl:value-of select="translate(String[Key='Notes']/Value, $lfcr, $delimiter)"/>
  160.         <!-- Add unknown strings -->
  161.         <xsl:for-each select="String">
  162.           <xsl:if test="(string(Key) != 'Title') and (string(Key) != 'UserName') and 
  163.            (string(Key) != 'Password') and (string(Key) != 'URL') and (string(Key) != 'Notes')">
  164.             <xsl:value-of select="$delimiter"/><xsl:value-of select="concat(Key,': ',translate(Value, $lfcr, $delimiter))"/>
  165.           </xsl:if>
  166.         </xsl:for-each>
  167.         <!-- override URL value if not empty -->
  168.         <xsl:if test="normalize-space(OverrideURL) != ''">
  169.           <xsl:value-of select="$delimiter"/><xsl:value-of select="concat('OverrideURL: ',OverrideURL)"/>
  170.         </xsl:if>
  171.         <!-- Tags -->
  172.         <xsl:if test="normalize-space(Tags) != ''">
  173.           <xsl:value-of select="$delimiter"/><xsl:value-of select="concat('Tags: ',Tags)"/>
  174.         </xsl:if>
  175.       </notes>
  176.  
  177.       <!-- UUID -->
  178.       <uuid>
  179.         <xsl:call-template name="base64ToHex">
  180.           <xsl:with-param name="val" select="UUID"/>
  181.         </xsl:call-template>
  182.       </uuid>
  183.  
  184.       <!-- autotype -->
  185.       <xsl:if test="string(AutoType/Enabled) = 'True'">
  186.         <xsl:variable name="autotype">
  187.           <xsl:choose>
  188.             <xsl:when test="normalize-space(AutoType/DefaultSequence) != ''">
  189.               <xsl:value-of select="AutoType/DefaultSequence"/>
  190.             </xsl:when>
  191.             <xsl:otherwise>
  192.               <xsl:value-of select="$groupAutotype"/>
  193.             </xsl:otherwise>
  194.           </xsl:choose>
  195.         </xsl:variable>
  196.         
  197.         <xsl:if test="$autotype != ''">
  198.           <autotype>
  199.             <xsl:call-template name="autotypeParser">
  200.               <xsl:with-param name="autotype" select="$autotype"/>
  201.             </xsl:call-template>
  202.           </autotype>
  203.         </xsl:if>
  204.       </xsl:if>
  205.  
  206.       <!-- times -->
  207.       <ctimex>
  208.         <xsl:value-of select="Times/CreationTime"/>
  209.       </ctimex>
  210.       <atimex>
  211.         <xsl:value-of select="Times/LastAccessTime"/>
  212.       </atimex>
  213.       <xsl:if test="string(Times/Expires) = 'True'">
  214.         <xtimex>
  215.           <xsl:value-of select="Times/ExpiryTime"/>
  216.         </xtimex>
  217.       </xsl:if>
  218.       <pmtimex>
  219.         <xsl:value-of select="Times/LastModificationTime"/>
  220.       </pmtimex>
  221.       <rmtimex>
  222.         <xsl:value-of select="Times/LastModificationTime"/>
  223.       </rmtimex>
  224.  
  225.       <!-- history -->
  226.       <pwhistory>
  227.         <status>1</status>
  228.         <max>
  229.           <xsl:value-of select="/KeePassFile/Meta/HistoryMaxItems"/>
  230.         </max>
  231.         <num>
  232.           <xsl:value-of select="count(History/Entry)"/>
  233.         </num>
  234.         <xsl:if test="count(History/Entry) > 0">
  235.           <history_entries>
  236.             <xsl:for-each select="History/Entry">
  237.               <xsl:call-template name="historyTemplate"/>
  238.             </xsl:for-each>
  239.           </history_entries>
  240.         </xsl:if>
  241.       </pwhistory>
  242.     </entry>
  243.   </xsl:template>
  244.  
  245. <!-- ====================================================================== -->
  246.   <!-- process history entries -->
  247.   <xsl:template name="historyTemplate">
  248.     <history_entry>
  249.       <xsl:attribute name="num">
  250.         <xsl:value-of select="position()"/>
  251.       </xsl:attribute>
  252.       <changedx>
  253.         <xsl:value-of select="Times/LastModificationTime"/>
  254.       </changedx>
  255.       <oldpassword>
  256.         <xsl:value-of select="String[Key='Password']/Value"/>
  257.       </oldpassword>
  258.     </history_entry>
  259.   </xsl:template>
  260.  
  261.  
  262.   <!-- process autotype sequences -->
  263.   <!-- 
  264.     Autotype: http://keepass.info/help/base/autotype.html
  265.     + Special key codes are case-insensitive
  266.     + Tab  {TAB} => \t
  267.     + +{TAB} => \s (Shift-Tab)
  268.     + Enter  {ENTER} or ~ => \n
  269.     * Arrow Up  {UP} 
  270.     * Arrow Down  {DOWN}
  271.     * Arrow Left  {LEFT}
  272.     * Arrow Right  {RIGHT}
  273.     * Insert  {INSERT} or {INS}
  274.     * Delete  {DELETE} or {DEL}
  275.     * Home  {HOME}
  276.     * End  {END}
  277.     * Page Up  {PGUP}
  278.     * Page Down  {PGDN}
  279.     + Backspace  {BACKSPACE}, {BS} or {BKSP} => /b
  280.     * Break  {BREAK}
  281.     * Caps-Lock  {CAPSLOCK}
  282.     * Escape  {ESC}
  283.     * Help  {HELP}
  284.     * Numlock  {NUMLOCK}
  285.     * Print Screen  {PRTSC}
  286.     * Scroll Lock  {SCROLLLOCK}
  287.     * F1 - F16  {F1} - {F16}
  288.     + Keypad +  {ADD} => +
  289.     + Keypad -  {SUBTRACT} => -
  290.     + Keypad *  {MULTIPLY} => *
  291.     + Keypad /  {DIVIDE} => /
  292.     * Shift  +
  293.     * Ctrl  ^
  294.     * Alt  %
  295.     + +  {+} => +
  296.     + ^  {^} => ^
  297.     + %  {%} => %
  298.     + ~  {~} => ~
  299.     + (, )  {(}, {)} => (, )
  300.     * {, }  {{}, {}} => {, }
  301.     + {DELAY X}  Delays X milliseconds. => \wX (\WX)
  302.     * {VKEY X}  Sends the virtual key of value X.
  303.     + {DELAY=X}  Sets the default delay to X milliseconds for all standard keypresses in this sequence. => \dX
  304.     + \ => \\
  305.     + Keys and special keys (not placeholders or commands) can be repeated by appending a number within the code. For example, {TAB 5} presses the Tab key 5 times. 
  306.   -->
  307.   <xsl:template name="autotypeParser">
  308.     <xsl:param name="autotype"/>
  309.     <!-- \ => \\  It should be done first -->
  310.     <xsl:variable name="repSlash">
  311.       <xsl:call-template name="replaceSubstring">
  312.         <xsl:with-param name="str" select="$autotype"/>
  313.         <xsl:with-param name="replace" select="'\'"/>
  314.         <xsl:with-param name="by" select="'\\'"/>
  315.       </xsl:call-template>
  316.     </xsl:variable>
  317.     <!-- ~ => \n -->
  318.     <xsl:variable name="repTilde">
  319.       <xsl:call-template name="replaceSubstring">
  320.         <xsl:with-param name="str" select="$repSlash"/>
  321.         <xsl:with-param name="replace" select="'~'"/>
  322.         <xsl:with-param name="by" select="'\n'"/>
  323.       </xsl:call-template>
  324.     </xsl:variable>
  325.     <!-- Fix {~} replaced with {\n}  -->
  326.     <xsl:variable name="repTilde2">
  327.       <xsl:call-template name="substSequence">
  328.         <xsl:with-param name="str" select="$repTilde"/>
  329.         <xsl:with-param name="tag" select="'\n'"/>
  330.         <xsl:with-param name="by" select="'~'"/>
  331.       </xsl:call-template>
  332.     </xsl:variable>
  333.     <!-- {ENTER} -->
  334.     <xsl:variable name="substEnter">
  335.       <xsl:call-template name="substSequence">
  336.         <xsl:with-param name="str" select="$repTilde2"/>
  337.         <xsl:with-param name="tag" select="'ENTER'"/>
  338.         <xsl:with-param name="by" select="'\n'"/>
  339.       </xsl:call-template>
  340.     </xsl:variable>
  341.     <!-- {TAB} -->
  342.     <xsl:variable name="substTab">
  343.       <xsl:call-template name="substSequence">
  344.         <xsl:with-param name="str" select="$substEnter"/>
  345.         <xsl:with-param name="tag" select="'TAB'"/>
  346.         <xsl:with-param name="by" select="'\t'"/>
  347.       </xsl:call-template>
  348.     </xsl:variable>
  349.     <!-- +\t -->
  350.     <xsl:variable name="repSTab">
  351.       <xsl:call-template name="replaceSubstring">
  352.         <xsl:with-param name="str" select="$substTab"/>
  353.         <xsl:with-param name="replace" select="'+\t'"/>
  354.         <xsl:with-param name="by" select="'\s'"/>
  355.       </xsl:call-template>
  356.     </xsl:variable>
  357.     <!-- {BACKSPACE} -->
  358.     <xsl:variable name="substBs">
  359.       <xsl:call-template name="substSequence">
  360.         <xsl:with-param name="str" select="$repSTab"/>
  361.         <xsl:with-param name="tag" select="'BACKSPACE'"/>
  362.         <xsl:with-param name="by" select="'\b'"/>
  363.       </xsl:call-template>
  364.     </xsl:variable>
  365.     <!-- {BS} -->
  366.     <xsl:variable name="substBs2">
  367.       <xsl:call-template name="substSequence">
  368.         <xsl:with-param name="str" select="$substBs"/>
  369.         <xsl:with-param name="tag" select="'BS'"/>
  370.         <xsl:with-param name="by" select="'\b'"/>
  371.       </xsl:call-template>
  372.     </xsl:variable>
  373.     <!-- {BKSP} -->
  374.     <xsl:variable name="substBs3">
  375.       <xsl:call-template name="substSequence">
  376.         <xsl:with-param name="str" select="$substBs2"/>
  377.         <xsl:with-param name="tag" select="'BKSP'"/>
  378.         <xsl:with-param name="by" select="'\b'"/>
  379.       </xsl:call-template>
  380.     </xsl:variable>
  381.     <!-- {ADD} -->
  382.     <xsl:variable name="substAdd">
  383.       <xsl:call-template name="substSequence">
  384.         <xsl:with-param name="str" select="$substBs3"/>
  385.         <xsl:with-param name="tag" select="'ADD'"/>
  386.         <xsl:with-param name="by" select="'+'"/>
  387.       </xsl:call-template>
  388.     </xsl:variable>
  389.     <!-- {+} -->
  390.     <xsl:variable name="substAdd2">
  391.       <xsl:call-template name="substSequence">
  392.         <xsl:with-param name="str" select="$substAdd"/>
  393.         <xsl:with-param name="tag" select="'+'"/>
  394.         <xsl:with-param name="by" select="'+'"/>
  395.       </xsl:call-template>
  396.     </xsl:variable>
  397.     <!-- {SUBTRACT} -->
  398.     <xsl:variable name="substSubtract">
  399.       <xsl:call-template name="substSequence">
  400.         <xsl:with-param name="str" select="$substAdd2"/>
  401.         <xsl:with-param name="tag" select="'SUBTRACT'"/>
  402.         <xsl:with-param name="by" select="'-'"/>
  403.       </xsl:call-template>
  404.     </xsl:variable>
  405.     <!-- {MULTIPLY} -->
  406.     <xsl:variable name="substMultiply">
  407.       <xsl:call-template name="substSequence">
  408.         <xsl:with-param name="str" select="$substSubtract"/>
  409.         <xsl:with-param name="tag" select="'MULTIPLY'"/>
  410.         <xsl:with-param name="by" select="'*'"/>
  411.       </xsl:call-template>
  412.     </xsl:variable>
  413.     <!-- {DIVIDE} -->
  414.     <xsl:variable name="substDivide">
  415.       <xsl:call-template name="substSequence">
  416.         <xsl:with-param name="str" select="$substMultiply"/>
  417.         <xsl:with-param name="tag" select="'DIVIDE'"/>
  418.         <xsl:with-param name="by" select="'/'"/>
  419.       </xsl:call-template>
  420.     </xsl:variable>
  421.     <!-- {^} -->
  422.     <xsl:variable name="substHat">
  423.       <xsl:call-template name="substSequence">
  424.         <xsl:with-param name="str" select="$substDivide"/>
  425.         <xsl:with-param name="tag" select="'^'"/>
  426.         <xsl:with-param name="by" select="'^'"/>
  427.       </xsl:call-template>
  428.     </xsl:variable>
  429.     <!-- {%} -->
  430.     <xsl:variable name="substPct">
  431.       <xsl:call-template name="substSequence">
  432.         <xsl:with-param name="str" select="$substHat"/>
  433.         <xsl:with-param name="tag" select="'%'"/>
  434.         <xsl:with-param name="by" select="'%'"/>
  435.       </xsl:call-template>
  436.     </xsl:variable>
  437.     <!-- {(} -->
  438.     <xsl:variable name="substRBrace">
  439.       <xsl:call-template name="substSequence">
  440.         <xsl:with-param name="str" select="$substPct"/>
  441.         <xsl:with-param name="tag" select="'('"/>
  442.         <xsl:with-param name="by" select="'('"/>
  443.       </xsl:call-template>
  444.     </xsl:variable>
  445.     <!-- {)} -->
  446.     <xsl:variable name="substLBrace">
  447.       <xsl:call-template name="substSequence">
  448.         <xsl:with-param name="str" select="$substRBrace"/>
  449.         <xsl:with-param name="tag" select="')'"/>
  450.         <xsl:with-param name="by" select="')'"/>
  451.       </xsl:call-template>
  452.     </xsl:variable>
  453.     <!-- {DELAY ...} -->
  454.     <xsl:variable name="substDelay">
  455.       <xsl:call-template name="substDelaySequence">
  456.         <xsl:with-param name="str" select="$substLBrace"/>
  457.       </xsl:call-template>
  458.     </xsl:variable>
  459.     <!-- substitute placeholders -->
  460.     <xsl:call-template name="substPlaceholder">
  461.       <xsl:with-param name="str" select="$substDelay"/>
  462.       <xsl:with-param name="inAutotype" select="1"/>
  463.     </xsl:call-template>
  464.   </xsl:template>
  465.  
  466.  
  467.   <!-- Placeholder substitution -->
  468.   <!--
  469.     Placeholders http://keepass.info/help/base/placeholders.html
  470.  
  471.     + Placeholders are case-insensitive.
  472.     + Title  {TITLE}
  473.     + User Name  {USERNAME}
  474.     +(non-autotype) URL  {URL}
  475.     + Password  {PASSWORD}
  476.     + Notes  {NOTES}
  477.     +(non-autotype as url) {URL:RMVSCM}  Entry URL without scheme specifier.
  478.     * {INTERNETEXPLORER}  Path of Internet Explorer, if installed.
  479.     * {FIREFOX}  Path of Mozilla Firefox, if installed.
  480.     * {OPERA}  Path of Opera, if installed.
  481.     * {GOOGLECHROME}  Path of Google Chrome, if installed.
  482.     +(non-autotype) {APPDIR}  KeePass application directory path.
  483.     + {GROUP}  Name of the entry's parent group.
  484.     +(non-autotype) {GROUPPATH}  Full group path of the entry.
  485.     +(non-autotype) {DB_PATH}  Full path of the current database.
  486.     +(non-autotype) {DB_DIR}  Directory of the current database.
  487.     +(non-autotype) {DB_NAME}  File name (including extension) of the current database.
  488.     +(non-autotype) {DB_BASENAME}  File name (excluding extension) of the current database.
  489.     +(non-autotype) {DB_EXT}  File name extension of the current database.
  490.     * {ENV_DIRSEP}  Directory separator ('\' on Windows, '/' on Unix).
  491.     * {DT_SIMPLE}  Current local date/time as a simple, sortable string.
  492.     * {DT_YEAR}  Year component of the current local date/time.
  493.     * {DT_MONTH}  Month component of the current local date/time.
  494.     * {DT_DAY}  Day component of the current local date/time.
  495.     * {DT_HOUR}  Hour component of the current local date/time.
  496.     * {DT_MINUTE}  Minute component of the current local date/time.
  497.     * {DT_SECOND}  Seconds component of the current local date/time.
  498.     * {DT_UTC_SIMPLE}  Current UTC date/time as a simple, sortable string.
  499.     * {DT_UTC_YEAR}  Year component of the current UTC date/time.
  500.     * {DT_UTC_MONTH}  Month component of the current UTC date/time.
  501.     * {DT_UTC_DAY}  Day component of the current UTC date/time.
  502.     * {DT_UTC_HOUR}  Hour component of the current UTC date/time.
  503.     * {DT_UTC_MINUTE}  Minute component of the current UTC date/time.
  504.     * {DT_UTC_SECOND}  Seconds component of the current UTC date/time.
  505.     * {PICKCHARS}
  506.     * {PICKCHARS:Fld:Opt}  Shows a dialog to pick certain characters from an entry string. See below.
  507.     * {NEWPASSWORD}  Generates a new password. See below.
  508.     * {PASSWORD_ENC}  Password in encrypted form. See below.
  509.     * {HMACOTP}  Generates a one-time password. See below.
  510.     +(non-autotype) $ => \$
  511.     +(non-autotype) \{ => \\{
  512.     + Custom strings can be referenced using {S:Name}. For example, if you have a custom string named "eMail", you can use the placeholder {S:eMail}. 
  513.   -->
  514.  
  515.   <xsl:template name="substPlaceholder">
  516.     <xsl:param name="str"/>
  517.     <xsl:param name="inAutotype"/><!-- if =1, than use sequences for autotype, else for cmd -->
  518.     <xsl:choose>
  519.       <xsl:when test="$inAutotype = 1" >
  520.         <!-- {TITLE} -->
  521.         <xsl:variable name="substTitle">
  522.           <xsl:call-template name="substSequence">
  523.             <xsl:with-param name="str" select="$str"/>
  524.             <xsl:with-param name="tag" select="'TITLE'"/>
  525.             <xsl:with-param name="by" select="'\i'"/>
  526.           </xsl:call-template>
  527.         </xsl:variable>
  528.         <!-- {USERNAME} -->
  529.         <xsl:variable name="substUsername">
  530.           <xsl:call-template name="substSequence">
  531.             <xsl:with-param name="str" select="$substTitle"/>
  532.             <xsl:with-param name="tag" select="'USERNAME'"/>
  533.             <xsl:with-param name="by" select="'\u'"/>
  534.           </xsl:call-template>
  535.         </xsl:variable>
  536.         <!-- {PASSWORD} -->
  537.         <xsl:variable name="substPassword">
  538.           <xsl:call-template name="substSequence">
  539.             <xsl:with-param name="str" select="$substUsername"/>
  540.             <xsl:with-param name="tag" select="'PASSWORD'"/>
  541.             <xsl:with-param name="by" select="'\p'"/>
  542.           </xsl:call-template>
  543.         </xsl:variable>
  544.         <!-- {NOTES} -->
  545.         <xsl:variable name="substNotes">
  546.           <xsl:call-template name="substSequence">
  547.             <xsl:with-param name="str" select="$substPassword"/>
  548.             <xsl:with-param name="tag" select="'NOTES'"/>
  549.             <xsl:with-param name="by" select="'\o'"/>
  550.           </xsl:call-template>
  551.         </xsl:variable>
  552.         <!-- {GROUP} -->
  553.         <xsl:variable name="substGroup">
  554.           <xsl:call-template name="substSequence">
  555.             <xsl:with-param name="str" select="$substNotes"/>
  556.             <xsl:with-param name="tag" select="'GROUP'"/>
  557.             <xsl:with-param name="by" select="'\g'"/>
  558.           </xsl:call-template>
  559.         </xsl:variable>
  560.         <!-- {S:FieldName} -->
  561.         <xsl:variable name="substF">
  562.           <xsl:call-template name="substField">
  563.             <xsl:with-param name="str" select="$substGroup"/>
  564.           </xsl:call-template>
  565.         </xsl:variable>
  566.         
  567.          <xsl:value-of select="$substF"/>
  568.       </xsl:when>
  569.       <xsl:otherwise>
  570.         <!-- \{ => \\{ For correct slash escaping after vars substitution. It should be done first -->
  571.         <xsl:variable name="repSlash">
  572.           <xsl:call-template name="replaceSubstring">
  573.             <xsl:with-param name="str" select="$str"/>
  574.             <xsl:with-param name="replace" select="'\{'"/>
  575.             <xsl:with-param name="by" select="'\\{'"/>
  576.           </xsl:call-template>
  577.         </xsl:variable>
  578.         <!-- $ => \$ Escape $. -->
  579.         <xsl:variable name="repS">
  580.           <xsl:call-template name="replaceSubstring">
  581.             <xsl:with-param name="str" select="$repSlash"/>
  582.             <xsl:with-param name="replace" select="'$'"/>
  583.             <xsl:with-param name="by" select="'\$'"/>
  584.           </xsl:call-template>
  585.         </xsl:variable>
  586.         <!-- {TITLE} -->
  587.         <xsl:variable name="substTitle">
  588.           <xsl:call-template name="substSequence">
  589.             <xsl:with-param name="str" select="$repS"/>
  590.             <xsl:with-param name="tag" select="'TITLE'"/>
  591.             <xsl:with-param name="by" select="'${t}'"/>
  592.           </xsl:call-template>
  593.         </xsl:variable>
  594.         <!-- {USERNAME} -->
  595.         <xsl:variable name="substUsername">
  596.           <xsl:call-template name="substSequence">
  597.             <xsl:with-param name="str" select="$substTitle"/>
  598.             <xsl:with-param name="tag" select="'USERNAME'"/>
  599.             <xsl:with-param name="by" select="'${u}'"/>
  600.           </xsl:call-template>
  601.         </xsl:variable>
  602.         <!-- {PASSWORD} -->
  603.         <xsl:variable name="substPassword">
  604.           <xsl:call-template name="substSequence">
  605.             <xsl:with-param name="str" select="$substUsername"/>
  606.             <xsl:with-param name="tag" select="'PASSWORD'"/>
  607.             <xsl:with-param name="by" select="'${p}'"/>
  608.           </xsl:call-template>
  609.         </xsl:variable>
  610.         <!-- {NOTES} -->
  611.         <xsl:variable name="substNotes">
  612.           <xsl:call-template name="substSequence">
  613.             <xsl:with-param name="str" select="$substPassword"/>
  614.             <xsl:with-param name="tag" select="'NOTES'"/>
  615.             <xsl:with-param name="by" select="'${n}'"/>
  616.           </xsl:call-template>
  617.         </xsl:variable>
  618.         <!-- {URL} -->
  619.         <xsl:variable name="substUrl">
  620.           <xsl:call-template name="substSequence">
  621.             <xsl:with-param name="str" select="$substNotes"/>
  622.             <xsl:with-param name="tag" select="'URL'"/>
  623.             <xsl:with-param name="by" select="'${url}'"/>
  624.           </xsl:call-template>
  625.         </xsl:variable>
  626.         <!-- {URL:RMVSCM} -->
  627.         <xsl:variable name="substUrl2">
  628.           <xsl:call-template name="substSequence">
  629.             <xsl:with-param name="str" select="$substUrl"/>
  630.             <xsl:with-param name="tag" select="'URL:RMVSCM'"/>
  631.             <xsl:with-param name="by" select="'${url}'"/>
  632.           </xsl:call-template>
  633.         </xsl:variable>
  634.         <!-- {APPDIR} -->
  635.         <xsl:variable name="substAppdir">
  636.           <xsl:call-template name="substSequence">
  637.             <xsl:with-param name="str" select="$substUrl2"/>
  638.             <xsl:with-param name="tag" select="'APPDIR'"/>
  639.             <xsl:with-param name="by" select="'${appdir}'"/>
  640.           </xsl:call-template>
  641.         </xsl:variable>
  642.         <!-- {GROUP} -->
  643.         <xsl:variable name="substGroup">
  644.           <xsl:call-template name="substSequence">
  645.             <xsl:with-param name="str" select="$substAppdir"/>
  646.             <xsl:with-param name="tag" select="'GROUP'"/>
  647.             <xsl:with-param name="by" select="'${G}'"/>
  648.           </xsl:call-template>
  649.         </xsl:variable>
  650.         <!-- {GROUPPATH} -->
  651.         <xsl:variable name="substFullGroup">
  652.           <xsl:call-template name="substSequence">
  653.             <xsl:with-param name="str" select="$substGroup"/>
  654.             <xsl:with-param name="tag" select="'GROUPPATH'"/>
  655.             <xsl:with-param name="by" select="'${g}'"/>
  656.           </xsl:call-template>
  657.         </xsl:variable>
  658.         <!-- {DB_PATH} -->
  659.         <xsl:variable name="substDBPath">
  660.           <xsl:call-template name="substSequence">
  661.             <xsl:with-param name="str" select="$substFullGroup"/>
  662.             <xsl:with-param name="tag" select="'DB_PATH'"/>
  663.             <xsl:with-param name="by" select="'${fulldb}'"/>
  664.           </xsl:call-template>
  665.         </xsl:variable>
  666.         <!-- {DB_DIR} -->
  667.         <xsl:variable name="substDBDir">
  668.           <xsl:call-template name="substSequence">
  669.             <xsl:with-param name="str" select="$substDBPath"/>
  670.             <xsl:with-param name="tag" select="'DB_DIR'"/>
  671.             <xsl:with-param name="by" select="'${dbdir}'"/>
  672.           </xsl:call-template>
  673.         </xsl:variable>
  674.         <!-- {DB_NAME} -->
  675.         <xsl:variable name="substDBName">
  676.           <xsl:call-template name="substSequence">
  677.             <xsl:with-param name="str" select="$substDBDir"/>
  678.             <xsl:with-param name="tag" select="'DB_NAME'"/>
  679.             <xsl:with-param name="by" select="concat('${dbname}','.','${dbextn}')"/>
  680.           </xsl:call-template>
  681.         </xsl:variable>
  682.         <!-- {DB_BASENAME} -->
  683.         <xsl:variable name="substDBBaseName">
  684.           <xsl:call-template name="substSequence">
  685.             <xsl:with-param name="str" select="$substDBName"/>
  686.             <xsl:with-param name="tag" select="'DB_BASENAME'"/>
  687.             <xsl:with-param name="by" select="'${dbname}'"/>
  688.           </xsl:call-template>
  689.         </xsl:variable>
  690.         <!-- {DB_EXT} -->
  691.         <xsl:variable name="substDBExt">
  692.           <xsl:call-template name="substSequence">
  693.             <xsl:with-param name="str" select="$substDBBaseName"/>
  694.             <xsl:with-param name="tag" select="'DB_EXT'"/>
  695.             <xsl:with-param name="by" select="'${dbextn}'"/>
  696.           </xsl:call-template>
  697.         </xsl:variable>
  698.         <!-- {S:FieldName} -->
  699.         <xsl:variable name="substF">
  700.           <xsl:call-template name="substField">
  701.             <xsl:with-param name="str" select="$substDBExt"/>
  702.           </xsl:call-template>
  703.         </xsl:variable>
  704.  
  705.          <xsl:value-of select="$substF"/>
  706.       </xsl:otherwise>
  707.     </xsl:choose>
  708.   </xsl:template>
  709.  
  710.   <!-- process url field -->
  711.   <!-- 
  712.     URL Field Capabilities: http://keepass.info/help/base/autourl.html
  713.     + To tell KeePass that the line you entered is a command line, prefix it using cmd://.
  714.     + Windows-style UNC paths (starting with \\) are directly supported, i.e. do not need to be prefixed with cmd://.
  715.     +(cmd-only) In the URL field, you can use several placeholders that will get automatically replaced when the URL is executed.
  716.     + {S:FieldName} subst. in URL
  717.   -->
  718.   <xsl:template name="urlParser">
  719.     <xsl:param name="url"/>
  720.     <xsl:param name="useAlt" select="0"/>
  721.     <xsl:choose>
  722.       <xsl:when test="substring($url, 1, 2) = '\\'">
  723.         <runcommand>
  724.           <!-- substitute placeholders -->
  725.           <xsl:call-template name="substPlaceholder">
  726.             <xsl:with-param name="str" select="$url"/>
  727.             <xsl:with-param name="inAutotype" select="0"/>
  728.           </xsl:call-template>
  729.         </runcommand>
  730.       </xsl:when>
  731.       <xsl:when test="translate(substring($url, 1, 6), $loCase, $upCase) = 'CMD://'">
  732.         <runcommand>
  733.           <!-- substitute placeholders -->
  734.           <xsl:call-template name="substPlaceholder">
  735.             <xsl:with-param name="str" select="substring($url, 7)"/>
  736.             <xsl:with-param name="inAutotype" select="0"/>
  737.           </xsl:call-template>
  738.         </runcommand>
  739.       </xsl:when>
  740.       <xsl:otherwise>
  741.         <url>
  742.           <!-- {S:FieldName} -->
  743.           <xsl:variable name="substF">
  744.             <xsl:call-template name="substField">
  745.               <xsl:with-param name="str" select="$url"/>
  746.             </xsl:call-template>
  747.           </xsl:variable>
  748.           <xsl:choose>
  749.             <xsl:when test="$useAlt = 1">
  750.               <xsl:value-of select="concat('[alt]',$substF)"/>
  751.             </xsl:when>
  752.             <xsl:otherwise>
  753.               <xsl:value-of select="$substF"/>
  754.             </xsl:otherwise>
  755.           </xsl:choose>
  756.         </url>
  757.       </xsl:otherwise>
  758.     </xsl:choose>
  759.   </xsl:template>
  760.   
  761.   <!-- convert base64Binary to hexBinary -->
  762.   <xsl:template name="base64ToHex">
  763.     <xsl:param name="val"/>
  764.     <xsl:choose>
  765.       <xsl:when test="string-length($val) mod 4 != 0">
  766.         Invalid Base64 string
  767.       </xsl:when>
  768.       <xsl:when test="string-length($val) > 0">
  769.         <xsl:call-template name="parseQuad">
  770.           <xsl:with-param name="quad" select="substring($val, 1, 4)"/>
  771.         </xsl:call-template>
  772.         <xsl:call-template name="base64ToHex">
  773.           <xsl:with-param name="val" select="substring($val, 5)"/>
  774.         </xsl:call-template>
  775.       </xsl:when>
  776.     </xsl:choose>
  777.   </xsl:template>
  778.  
  779.   <!-- parse four letters in base64  -->
  780.   <xsl:template name="parseQuad">
  781.     <xsl:param name="quad"/>
  782.     <xsl:variable name="digs" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='"/>
  783.  
  784.     <xsl:variable name="v1" select="string-length(substring-before($digs, substring($quad, 1, 1)))"/>
  785.     <xsl:variable name="v2" select="string-length(substring-before($digs, substring($quad, 2, 1)))"/>
  786.     <xsl:variable name="v3" select="string-length(substring-before($digs, substring($quad, 3, 1)))"/>
  787.     <xsl:variable name="v4" select="string-length(substring-before($digs, substring($quad, 4, 1)))"/>
  788.  
  789.     <xsl:variable name="res" select="$v1*262144 + $v2*4096 + $v3*64 + $v4"/> <!-- v1 <<18 + v2 << 12 + v3 << 6 + v4 -->
  790.  
  791.     <xsl:variable name="r1" select="floor($res div 65536)"/>
  792.     <xsl:variable name="b1" select="$r1 mod 255"/>
  793.  
  794.     <xsl:variable name="r2" select="floor(($res - $r1*65536) div 256)"/>
  795.     <xsl:variable name="b2" select="$r2 mod 255"/>
  796.  
  797.     <xsl:variable name="r3" select="$res - $r1*65536 - $r2*256"/>
  798.     <xsl:variable name="b3" select="$r3"/>
  799.  
  800.     <xsl:call-template name="decToHex">
  801.       <xsl:with-param name="val" select="$b1"/>
  802.     </xsl:call-template>
  803.     <xsl:if test="$v3 != 64">
  804.       <xsl:call-template name="decToHex">
  805.         <xsl:with-param name="val" select="$b2"/>
  806.       </xsl:call-template>
  807.     </xsl:if>
  808.     <xsl:if test="$v4 != 64">
  809.       <xsl:call-template name="decToHex">
  810.         <xsl:with-param name="val" select="$b3"/>
  811.       </xsl:call-template>
  812.     </xsl:if>
  813.   </xsl:template>
  814.  
  815.    <!-- convert decimal value (less than 256) to hexadecimal -->
  816.   <xsl:template name="decToHex">
  817.     <xsl:param name="val"/>
  818.  
  819.     <xsl:variable name="digs" select="'0123456789ABCDEF'"/>
  820.  
  821.     <xsl:value-of select="substring($digs, floor($val div 16)+1, 1)"/>
  822.     <xsl:value-of select="substring($digs, floor($val mod 16)+1, 1)"/>
  823.   </xsl:template>
  824.  
  825.  
  826.   <!-- substitutions for tag sequnces {TAG count} -->
  827.   <xsl:template name="substSequence">
  828.     <xsl:param name="str"/>
  829.     <xsl:param name="tag"/>
  830.     <xsl:param name="by"/>
  831.     <xsl:param name="caseSensitive" select="0"/>
  832.  
  833.     <xsl:variable name="openTag" select="'{'"/>
  834.     <xsl:variable name="closeTag" select="'}'"/>
  835.  
  836.     <!-- convert tag for search obeying caseSensitive parameter -->
  837.     <xsl:variable name="sTag">
  838.       <xsl:choose>
  839.         <xsl:when test="$caseSensitive = 1">
  840.           <xsl:value-of select="concat($openTag, $tag)"/>
  841.         </xsl:when>
  842.         <xsl:otherwise>
  843.           <xsl:value-of select="concat($openTag, translate($tag,$loCase,$upCase))"/>
  844.         </xsl:otherwise>
  845.       </xsl:choose>
  846.     </xsl:variable>
  847.  
  848.     <!-- convert string for search obeying caseSensitive parameter -->
  849.     <xsl:variable name="sStr">
  850.       <xsl:choose>
  851.         <xsl:when test="$caseSensitive = 1">
  852.           <xsl:value-of select="$str"/>
  853.         </xsl:when>
  854.         <xsl:otherwise>
  855.           <xsl:value-of select="translate($str,$loCase,$upCase)"/>
  856.         </xsl:otherwise>
  857.       </xsl:choose>
  858.     </xsl:variable>
  859.     
  860.     <xsl:choose>
  861.       <xsl:when test="contains($sStr, $sTag)">
  862.         <xsl:value-of select="substring($str, 1, string-length(substring-before($sStr, $sTag)))"/>
  863.         <xsl:variable name="tail" select="substring($str, string-length(substring-before($sStr, $sTag))+string-length($sTag)+1)"/>
  864.         <!-- get count (empty equal to one) -->
  865.         <xsl:variable name="cnt" select="normalize-space(substring-before($tail, $closeTag))"/>
  866.         <xsl:choose>
  867.           <xsl:when test="$cnt = ''">
  868.             <xsl:value-of select="$by"/>
  869.           </xsl:when>
  870.           <xsl:otherwise>
  871.             <xsl:call-template name="repeatString">
  872.               <xsl:with-param name="str" select="$by"/>
  873.               <xsl:with-param name="n" select="$cnt"/>
  874.             </xsl:call-template>
  875.           </xsl:otherwise>
  876.         </xsl:choose>
  877.         <xsl:call-template name="substSequence">
  878.           <xsl:with-param name="str" select="substring-after($tail, $closeTag)"/>
  879.           <xsl:with-param name="tag" select="$tag"/>
  880.           <xsl:with-param name="by" select="$by"/>
  881.           <xsl:with-param name="caseSensitive" select="$caseSensitive"/>
  882.         </xsl:call-template>
  883.       </xsl:when>
  884.       <xsl:otherwise>
  885.         <xsl:value-of select="$str"/>
  886.       </xsl:otherwise>
  887.     </xsl:choose>
  888.   </xsl:template>
  889.  
  890.   <!-- repeat given string n times -->
  891.   <xsl:template name="repeatString">
  892.     <xsl:param name="str"/>
  893.     <xsl:param name="n"/>
  894.  
  895.     <xsl:if test="$n > 0">
  896.       <xsl:value-of select="$str"/>
  897.       <xsl:call-template name="repeatString">
  898.         <xsl:with-param name="str" select="$str"/>
  899.         <xsl:with-param name="n" select="$n - 1"/>
  900.       </xsl:call-template>
  901.     </xsl:if>
  902.   </xsl:template>
  903.    
  904.   <!-- substitutions for {DELAY X} and {DELAY=X} tag sequnces -->
  905.   <xsl:template name="substDelaySequence">
  906.     <xsl:param name="str"/>
  907.     <xsl:param name="caseSensitive" select="0"/>
  908.  
  909.     <xsl:variable name="tag" select="'DELAY'"/>
  910.     <xsl:variable name="openTag" select="'{'"/>
  911.     <xsl:variable name="closeTag" select="'}'"/>
  912.  
  913.     <!-- convert tag for search obeying caseSensitive parameter -->
  914.     <xsl:variable name="sTag">
  915.       <xsl:choose>
  916.         <xsl:when test="$caseSensitive = 1">
  917.           <xsl:value-of select="concat($openTag, $tag)"/>
  918.         </xsl:when>
  919.         <xsl:otherwise>
  920.           <xsl:value-of select="concat($openTag, translate($tag,$loCase,$upCase))"/>
  921.         </xsl:otherwise>
  922.       </xsl:choose>
  923.     </xsl:variable>
  924.  
  925.     <!-- convert string for search obeying caseSensitive parameter -->
  926.     <xsl:variable name="sStr">
  927.       <xsl:choose>
  928.         <xsl:when test="$caseSensitive = 1">
  929.           <xsl:value-of select="$str"/>
  930.         </xsl:when>
  931.         <xsl:otherwise>
  932.           <xsl:value-of select="translate($str,$loCase,$upCase)"/>
  933.         </xsl:otherwise>
  934.       </xsl:choose>
  935.     </xsl:variable>
  936.  
  937.     <xsl:choose>
  938.       <xsl:when test="contains($sStr, $sTag)">
  939.         <xsl:value-of select="substring($str, 1, string-length(substring-before($sStr, $sTag)))"/>
  940.         <xsl:variable name="tail" select="substring($str, string-length(substring-before($sStr, $sTag))+string-length($sTag)+1)"/>
  941.         <!-- get count-->
  942.         <xsl:variable name="cnt" select="normalize-space(substring-before($tail, $closeTag))"/>
  943.         <xsl:choose>
  944.           <xsl:when test="substring($cnt,1,1) = '='">
  945.             <xsl:value-of select="concat('\d',substring($cnt,2))"/>
  946.           </xsl:when>
  947.           <xsl:when test="$cnt > 999">
  948.             <xsl:value-of select="concat('\W',floor($cnt div 1000))"/>
  949.             <xsl:if test="$cnt mod 1000 > 0">
  950.               <xsl:value-of select="concat('\w',$cnt mod 1000)"/>
  951.             </xsl:if>
  952.           </xsl:when>
  953.           <xsl:otherwise>
  954.             <xsl:value-of select="concat('\w',$cnt)"/>
  955.           </xsl:otherwise>
  956.         </xsl:choose>
  957.         <xsl:call-template name="substDelaySequence">
  958.           <xsl:with-param name="str" select="substring-after($tail, $closeTag)"/>
  959.           <xsl:with-param name="caseSensitive" select="$caseSensitive"/>
  960.         </xsl:call-template>
  961.       </xsl:when>
  962.       <xsl:otherwise>
  963.         <xsl:value-of select="$str"/>
  964.       </xsl:otherwise>
  965.     </xsl:choose>
  966.   </xsl:template>
  967.  
  968.   <!-- substitute local string fields {S:FieldName} -->
  969.   <xsl:template name="substField">
  970.     <xsl:param name="str"/>
  971.     
  972.     <xsl:variable name="tag" select="'S:'" />
  973.     <xsl:variable name="openTag" select="'{'" />
  974.     <xsl:variable name="closeTag" select="'}'" />
  975.  
  976.     <xsl:variable name="sTag" select="concat($openTag,$tag)" />
  977.  
  978.     <xsl:choose>
  979.       <xsl:when test="contains($str, $sTag)">
  980.         <xsl:value-of select="substring-before($str, $sTag)"/>
  981.         <xsl:variable name="tail" select="substring-after($str, $sTag)"/>
  982.         <!-- get field name -->
  983.         <xsl:variable name="fName" select="substring-before($tail, $closeTag)"/>
  984.         <xsl:value-of select="String[Key=$fName]/Value"/>
  985.         
  986.         <xsl:call-template name="substField">
  987.           <xsl:with-param name="str" select="substring-after($tail, $closeTag)"/>
  988.         </xsl:call-template>
  989.       </xsl:when>
  990.       <xsl:otherwise>
  991.         <xsl:value-of select="$str"/>
  992.       </xsl:otherwise>
  993.     </xsl:choose>
  994.   </xsl:template>
  995.  
  996.   <!-- replace all substrings -->
  997.   <xsl:template name="replaceSubstring">
  998.     <xsl:param name="str"/>
  999.     <xsl:param name="replace"/>
  1000.     <xsl:param name="by"/>
  1001.     <xsl:param name="caseSensitive" select="1"/>
  1002.  
  1003.     <!-- convert search string obeying caseSensitive parameter -->
  1004.     <xsl:variable name="sReplace">
  1005.       <xsl:choose>
  1006.         <xsl:when test="$caseSensitive = 1">
  1007.           <xsl:value-of select="$replace"/>
  1008.         </xsl:when>
  1009.         <xsl:otherwise>
  1010.           <xsl:value-of select="translate($replace,$loCase,$upCase)"/>
  1011.         </xsl:otherwise>
  1012.       </xsl:choose>
  1013.     </xsl:variable>
  1014.  
  1015.     <!-- convert string obeying caseSensitive parameter -->
  1016.     <xsl:variable name="sStr">
  1017.       <xsl:choose>
  1018.         <xsl:when test="$caseSensitive = 1">
  1019.           <xsl:value-of select="$str"/>
  1020.         </xsl:when>
  1021.         <xsl:otherwise>
  1022.           <xsl:value-of select="translate($str,$loCase,$upCase)"/>
  1023.         </xsl:otherwise>
  1024.       </xsl:choose>
  1025.     </xsl:variable>
  1026.     
  1027.     <xsl:choose>
  1028.       <xsl:when test="contains($sStr, $sReplace)">
  1029.         <xsl:value-of select="substring($str, 1, string-length(substring-before($sStr, $sReplace)))"/>
  1030.         <xsl:value-of select="$by"/>
  1031.         <xsl:call-template name="replaceSubstring">
  1032.           <xsl:with-param name="str" select="substring($str, string-length(substring-before($sStr, $sReplace))+string-length($sReplace)+1)"/>
  1033.           <xsl:with-param name="replace" select="$replace"/>
  1034.           <xsl:with-param name="by" select="$by"/>
  1035.         </xsl:call-template>
  1036.       </xsl:when>
  1037.       <xsl:otherwise>
  1038.         <xsl:value-of select="$str"/>
  1039.       </xsl:otherwise>
  1040.     </xsl:choose>
  1041.   </xsl:template>
  1042.  
  1043. </xsl:stylesheet>
  1044.